﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace SHRP.UI.Command
{
    public static class CommandManager
    {
        public static Dictionary<CommandFamily, CommandBase> commandlist = new Dictionary<CommandFamily, CommandBase>();

        public static CommandBase emptycommand = new EmptyCommand();

        static CommandManager()
        {
            LoadCommandFormFile();
        }

        public static void LoadCommandFormFile()
        {
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            foreach (Type t in types)
            {
                if(t.IsSubclassOf(typeof(CommandBase)))
                {
                    CommandBase newcommand=Activator.CreateInstance(t) as CommandBase;
                    if (newcommand!=null)
                    {
                        RegisterCommand(newcommand);
                    }
                }
            }
        }

        public static CommandBase GetCommand(CommandFamily Family)
        {
            if (commandlist.ContainsKey(Family))
            {
                return commandlist[Family];
            }
            else
            {
                return emptycommand;
            }
        }

        internal static void RegisterCommand(CommandBase command)
        {
            if (!commandlist.ContainsKey(command.Family))
            {
                commandlist.Add(command.Family, command);
            }
        }
    }
}
